package com.ma.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ma.gulimall.coupon.dao.SkuFullReductionDao;
import com.ma.gulimall.coupon.entity.MemberPriceEntity;
import com.ma.gulimall.coupon.entity.SkuFullReductionEntity;
import com.ma.gulimall.coupon.entity.SkuLadderEntity;
import com.ma.gulimall.coupon.service.MemberPriceService;
import com.ma.gulimall.coupon.service.SkuFullReductionService;
import com.ma.gulimall.coupon.service.SkuLadderService;
import com.ma.gulimall.to.MemberPrice;
import com.ma.gulimall.to.SkuReductionTo;
import com.ma.gulimall.utils.PageUtils;
import com.ma.gulimall.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {

        //sku的优惠，满减信息
        /** 一、SKU打折信息
         *  表`sms_sku_ladder` -- 实体类 SkuLadderEntity
         *  需要数据：   sku_id   full_count  discount  price  add_other
         *  SkuReductionTo已经有的sku_id fullCount discount countStatus=add_other
         * */
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo,skuLadderEntity);
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        if(skuReductionTo.getFullCount()>0){
            skuLadderService.save(skuLadderEntity);
        }

        /**
         * 二、SKU满减信息
         * 表`sms_sku_full_reduction` 实体类 SkuFullReductionEntity
         * 直接对拷
         * */
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
        if(skuFullReductionEntity.getFullPrice().compareTo(BigDecimal.ZERO)==1){
            this.save(skuFullReductionEntity);
        }

        /**
         * 三、SKU会员价格
         *      SkuReductionTo中封装了   List<MemberPrice> memberPrice;
         *  表 `sms_member_price` 实体类  MemberPriceEntity
         *  需要数据sku_id   member_level_id   member_level_name  member_price   add_other
         *  MemberPrice中拥有的数据  id=member_level_id    name=member_level_name  price=member_price
         *  skuId在skuReductionTo中
         *  add_other暂时设置为0
         * */
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(mem -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberPrice(mem.getPrice());
            memberPriceEntity.setMemberLevelId(mem.getId());
            memberPriceEntity.setMemberLevelName(mem.getName());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->{
            return item.getMemberPrice().compareTo(BigDecimal.ZERO)==1;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(collect);


    }

}